package com.nopay.cdxphone.ui.ativity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.nopay.cdxphone.base.BaseActivity;

import com.nopay.cdxphone.databinding.ActivityAuthorityBinding;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: 更新用户姓名
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class AuthorityActivity extends BaseActivity<ActivityAuthorityBinding> {

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(AuthorityActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSwipeBackHelper.backward();
            }
        });
        binding.rlContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAppSetting();

            }
        });
        binding.rlTel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAppSetting();
            }
        });
        binding.rlTelRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAppSetting();
            }
        });
        binding.rlRecordSound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAppSetting();
            }
        });
        binding.rlDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAppSetting();
            }
        });
        binding.rlFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAppSetting();
            }
        });
        binding.rlCamara.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAppSetting();
            }
        });
    }

    private void gotoAppSetting() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(localIntent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getPermissionInfo("android.permission.READ_CONTACTS", binding.tvContactText);
        getPermissionInfo("android.permission.CALL_PHONE", binding.tvTelText);
        getPermissionInfo("android.permission.READ_CALL_LOG", binding.tvTelRecordText);
        getPermissionInfo("android.permission.RECORD_AUDIO", binding.tvRecordSoundText);
        getPermissionInfo("android.permission.READ_PHONE_STATE", binding.tvDeviceInfoText);
        getPermissionInfo("android.permission.WRITE_EXTERNAL_STORAGE", binding.tvFileText);
        getPermissionInfo("android.permission-group.CAMERA", binding.tvCamaraText);
    }

    private void getPermissionInfo(String permissionName, TextView textView) {
        PackageManager pm = getPackageManager();
        boolean permission = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission(permissionName, "cn.cn.myvisualphone"));
        if (permission) {
            textView.setText("已获取");
        } else {
            textView.setText("未获取");
        }
    }

}
